package com.molyfun.parents.activity.setting.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.DayCourseListActivity;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.activity.msg.MsgActivity;
import com.molyfun.parents.activity.msg.MsgDetailActivity;
import com.molyfun.parents.activity.msg.PushNoticeActivity;
import com.molyfun.parents.activity.msg.RefundDetailActivity;
import com.molyfun.parents.activity.msg.adapter.MsgListAdapter;
import com.molyfun.parents.activity.report.ReportDetailActivity;
import com.molyfun.parents.activity.report.ReportResultActivity;
import com.molyfun.parents.activity.report.plan.PlanDetailActivity;
import com.molyfun.parents.push.PushMsgCacheCenter;
import com.molyfun.parents.rest.bean.MsgListResponse;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import mabeijianxi.camera.util.Log;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/3/16.
 * <p>
 * 此类为容器
 */
public class MsgItemView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.ll_msgview)
    LinearLayout mll_msgview;
    @InjectView(R.id.msg_dot)
    ImageView mMsgDot;
    @InjectView(R.id.msg_content)
    TextView mMsgContent;
    @InjectView(R.id.msg_from_time)
    TextView mMsgFromTime;
    @InjectView(R.id.topic_checkbox)
    ImageView mTopicheckbox;
    @InjectView(R.id.msg_title)
    TextView mMsgTitle;

    @InjectView(R.id.line)
    View mLine;
    @InjectView(R.id.bottom_layout)
    RelativeLayout mBottomLayout;


    private MsgListResponse.MsgBean mMsgBean;
    public String TAG = "MsgItemView";

    public interface MesgDataMode {
        int MODE_OFFLINE = 1001;
        int MODE_ONLINE = 1002;
        int MODE_BOTH = 1003;
    }

    private int mCurrentMsgDataMode = MsgItemView.MesgDataMode.MODE_ONLINE;


    public MsgItemView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public MsgItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MsgItemView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        Log.i(TAG, "mMsgItemView.init");
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_msg_item, this);
        ButterKnife.inject(this);
        mll_msgview.setOnClickListener(this);

    }

    /**
     * init view
     */
    public void initData(MsgListResponse.MsgBean msgBean) {
        initData(msgBean, mCurrentMsgDataMode, MsgListAdapter.MsgMode.MODE_NORMAL, false);
    }

    private int mViewMode = MsgListAdapter.MsgMode.MODE_NORMAL;

    public void initData(MsgListResponse.MsgBean msgBean, int msgDataMode, int viewMode) {
        initData(msgBean, msgDataMode, viewMode, false);
    }

    public void initData(MsgListResponse.MsgBean msgBean, int msgDataMode, int viewMode, boolean isLast) {
        Log.i(TAG, "mMsgItemView.initData");
        mCurrentMsgDataMode = msgDataMode;
        mViewMode = viewMode;

        if (msgBean != null) {
            mMsgBean = msgBean;
            mMsgContent.setText(msgBean.getContent());
            mMsgFromTime.setText(msgBean.getFromusername() + " " + TimeUtils.transForDate(msgBean.getCreatedatetime()));
            if (TextUtils.isEmpty(msgBean.getIsread()) || msgBean.getIsread().equals("0")) {
                mMsgDot.setVisibility(View.VISIBLE);
            } else {
                mMsgDot.setVisibility(View.INVISIBLE);
            }
            mMsgTitle.setText(msgBean.getTheme());
//            mRecordText.setText(recordBean.getContent());
        }
        if (viewMode == MsgListAdapter.MsgMode.MODE_CHOOSE) {
            //控制复选框
            mTopicheckbox.setVisibility(View.VISIBLE);
            mTopicheckbox.setOnClickListener(this);


            if (mMsgBean.isChecked()) {
                mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_checked);
            } else {
                mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_unchecked);
            }

        } else {
            mMsgBean.setIsChecked(false);
            mTopicheckbox.setVisibility(View.INVISIBLE);
        }

        if(isLast){
            mLine.setVisibility(View.GONE);
//            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mBottomLayout.getLayoutParams();
//            layoutParams.bottomMargin = getResources().getDimensionPixelOffset(R.dimen.common_bottom_bar);
//            mBottomLayout.setLayoutParams(layoutParams);
        }else{
            mLine.setVisibility(View.VISIBLE);
//            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mBottomLayout.getLayoutParams();
//            layoutParams.bottomMargin =0;
//            mBottomLayout.setLayoutParams(layoutParams);
        }

    }

    /**
     * GXAPZDJZD   //个训
     * BGSCJZD   //报告
     * PGXJGSCJZD  //评估
     * ZYAPZD   作业
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_msgview:
                if(mViewMode ==  MsgListAdapter.MsgMode.MODE_CHOOSE){
                    if (mMsgBean.isChecked()) {
                        mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_unchecked);
                        mMsgBean.setIsChecked(false);
                    } else {
                        mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_checked);
                        mMsgBean.setIsChecked(true);
                    }
                    if (msgItemViewCallback != null) {
                        msgItemViewCallback.onMsgCheckChanged(mMsgBean, mMsgBean.isChecked());
                    }
                }else{
                    if (TextUtils.isEmpty(mMsgBean.getIsread()) || mMsgBean.getIsread().equals("0")) {
                        PushMsgCacheCenter.getInstance().unReadCountMinus();
                    }
                    if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("TZ")) {
                        PushNoticeActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("GXAPZDJZD")) {
                        PlanDetailActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("BGSCJZD")) {
                        ReportDetailActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("PGXJGSCJZD")) {
                        ReportResultActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("TCSJTXJZD")) {
                        ProductDetailActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("TCXFYZTXJZD")) {
//                    ExpenseRecordActivity.startActivity(context);
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("RXTZ")) {
                        MsgDetailActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getDepartname(), TimeUtils.getDateByTimeMillis(mMsgBean.getCreatedatetime()), mMsgBean.getContent(), "RXTZ", mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("RXSB")) {
                        MsgDetailActivity.startActivity(context, mMsgBean.getDetailid(), mMsgBean.getDepartname(), TimeUtils.getDateByTimeMillis(mMsgBean.getCreatedatetime()), mMsgBean.getContent(), "RXSB", mMsgBean.getId());
                    } else if (!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("ZYAPZD")) {
                        //家庭作业不跳转页面
                    } else if(!TextUtils.isEmpty(mMsgBean.getCode()) && mMsgBean.getCode().equals("KTBX")) {
                        DayCourseListActivity.startActivity(getContext(), mMsgBean.getDetailid());
                    } else if(!TextUtils.isEmpty(mMsgBean.getCode()) && (mMsgBean.getCode().equals("DDTKSB") || mMsgBean.getCode().equals("DDTKCG"))) {
                        RefundDetailActivity.startActivity(getContext(), mMsgBean);
                    } else {
                        MsgActivity.startActivity(getContext(), mMsgBean);

                    }
                    if (msgItemViewHomeWork != null) {
                        msgItemViewHomeWork.OnMsgChangeState(mMsgBean.getId());
                    }
                    mMsgDot.setVisibility(View.INVISIBLE);
                    mMsgBean.setIsread("1");
                }
                break;
            case R.id.topic_checkbox:
                if (mMsgBean.isChecked()) {
                    mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_unchecked);
                    mMsgBean.setIsChecked(false);
                } else {
                    mTopicheckbox.setImageResource(R.mipmap.ic_checkbox_checked);
                    mMsgBean.setIsChecked(true);
                }
                if (msgItemViewCallback != null) {
                    msgItemViewCallback.onMsgCheckChanged(mMsgBean, mMsgBean.isChecked());
                }
                break;
            default:
                break;
        }

    }


    private MsgItemViewCallback msgItemViewCallback;

    public void setMsgItemViewCallback(MsgItemViewCallback msgItemViewCallback) {
        this.msgItemViewCallback = msgItemViewCallback;
    }

    public interface MsgItemViewCallback {
        void onMsgCheckChanged(MsgListResponse.MsgBean msgBean, boolean isChecked);
    }

    private MsgItemViewHomeWork msgItemViewHomeWork;

    public void setMsgItemViewCallbackMsgId(MsgItemViewHomeWork msgItemViewHomeWork) {
        this.msgItemViewHomeWork = msgItemViewHomeWork;
    }

    public interface MsgItemViewHomeWork {
        void OnMsgChangeState(String mMsgId);
    }
}